﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows;

namespace QLKS.Models
{
    static public class NhanVienModel
    {
        static public DataTable GetAllNhanVien(string selectColumns)
        {
            return DataProviderModel.QueryResults(string.Format("select {0} from dmNhanVien", selectColumns.Trim() == "" ? "*" : selectColumns)).Tables[0];
        }

        //Lấy tất cả nhân viên
        static public DataTable GetAllNhanVien()
        {
            return DataProviderModel.QueryResults(@"SELECT * from v_NhanVien").Tables[0];
        }

        //Lấy thông tin nhân viên
        static public DataTable GetNhanVien(string manv)
        {
            return DataProviderModel.QueryResults("select * from v_NhanVien WHERE manv = '" + manv + "'").Tables[0];
        }

        //Lấy thông tin nhân viên
        static public DataTable GetNhanVienTheoUserName(string username)
        {
            return DataProviderModel.QueryResults("select a.* from v_NhanVien a, users b WHERE a.manv = b.manhanvien AND b.username = '" + username + "'").Tables[0];
        }

        //Tạo mới nhân viên
        static public DataTable CreateNewNhanVien()
        {
            DataTable tb = DataProviderModel.QueryResults(@"SELECT * FROM v_NhanVien WHERE 1=0").Tables[0];
            DataRow row = tb.NewRow();
            row["manv"] = GetMaNhanVien();
            row["tennv"] = "";
            row["gioitinh"] = "Nam";
            row["cmnd"] = "";
            row["ngaysinh"] = DateTime.Today;
            row["diachi"] = "";
            row["dienthoai"] = "";
            row["email"] = "";
            row["macv"] = "nv";
            row["hinhanh"] = null;
            row["ghichu"] = "";
            tb.Rows.Add(row);
            return tb;
        }

        //
        static public string GetMaNhanVien()
        {
            object MaNV = DataProviderModel.ScalarExecute("SELECT MAX(manv) FROM dmNhanVien");
            int stt = 0;
            if (MaNV.GetType() == typeof(System.DBNull))
                stt = 0;
            else
                stt = int.Parse(MaNV.ToString().Substring(2));
            stt++;
            return "NV" + DataProviderModel.LeftPad(stt.ToString(), 5 - stt.ToString().Length, "0");
        }

        //Lấy Nhóm nhân viên
        static public DataTable GetNhomNhanVien()
        {
            return DataProviderModel.QueryResults("select * from ChucVu").Tables[0];
        }

        //Thêm nhân viên
        static public int ThemNhanVien(DataTable tb)
        {
            //DataTable tb = DataProviderModel.CreateColumns("dmNhanVien", "");
            //tb.Rows.Add(ma,hoten,gioitinh,cmnd,ngaysinh,diachi,dienthoai,email,chucvu,hinhanh,ghichu);
            string manv = tb.Rows[0]["manv"].ToString();
            if (GetAllNhanVien("manv").Select("manv ='" + manv + "'").Length <= 0)
                return DataProviderModel.Insert(tb, "dmNhanVien", "");
            else
                return DataProviderModel.Update(tb, "dmNhanVien", "manv");
        }

        //Xóa nhân viên
        static public int XoaNhanVien(string ma)
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("ma");
            tb.Rows.Add(ma);
            return DataProviderModel.Detele(tb, "dmNhanVien");
        }
    }
}
